﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleData.ObjectModel
{

	public class SchemaName : DataObjectNameBase
	{
		public const string DefaultSchemaNamespace = "Global.Schemas";

		protected SchemaName()
			: base()
		{ }

		public SchemaName( string name )
			: this( name, DefaultSchemaNamespace )
		{ }

		public SchemaName( string name, string ns )
			: base( name, ns )
		{ }


		public static bool TryParse( string fullName, out SchemaName schema )
		{
			schema = DataObjectNameParser.TryParse( fullName, ( ns, name ) => new SchemaName( name, ns ) );
			return schema != null;
		}

		public static SchemaName Parse( string fullName )
		{
			SchemaName name;
			if ( SchemaName.TryParse( fullName, out name ) )
				return name;
			else
				throw new InvalidOperationException( String.Format( "O schemaname \"{0}\" é inválido.", fullName ) );
		}
	}

	/// <summary>
	/// Representa a referência para o schema de uma entidade do sistema
	/// </summary>
	/// <remarks>
	/// Padrão da nomenclatura
	/// namespace[schemaName]
	/// </remarks>
	//public class SchemaName2 : IDataObjectName
	//{
	//    public const string DefaultSchemaNamespace = "Global.Schemas";
	//    private string fullName;
	//    //public static readonly SchemaName Global = new SchemaName( "Global" );
	//    //private static readonly DataObjectNameParser parser = new DataObjectNameParser();

	//    /// <summary>
	//    /// Utilizado pelo EF
	//    /// </summary>
	//    protected SchemaName2()
	//    { }

	//    public SchemaName2( string name )
	//        : this( name, DefaultSchemaNamespace )
	//    { }

	//    public SchemaName2( string name, string ns )
	//    {
	//        this.Name = name;
	//        this.Namespace = ns;
	//        //this.FullName = String.Concat( ns, "[", name, "]" );
	//    }

	//    /// <summary>
	//    /// Obtém o nome da entidade
	//    /// </summary>
	//    public string Name
	//    { get; private set; }

	//    /// <summary>
	//    /// Obtém o namespace em que está localizada essa entidade
	//    /// </summary>
	//    public string Namespace
	//    { get; private set; }

	//    /// <summary>
	//    /// 
	//    /// </summary>
	//    public string FullName
	//    {
	//        get
	//        {
	//            if ( this.fullName == null )
	//                this.fullName = String.Concat( this.Namespace, "[", this.Name, "]" );
	//            return this.fullName;
	//        }
	//    }

	//    public override bool Equals( object obj )
	//    {
	//        SchemaName schema = obj as SchemaName;

	//        if ( schema == null )
	//            return false;
	//        else
	//            return this.FullName.Equals( schema.FullName, StringComparison.InvariantCultureIgnoreCase );
	//    }

	//    public override int GetHashCode()
	//    {
	//        return this.FullName.GetHashCode();
	//    }

	//    public override string ToString()
	//    {
	//        return this.FullName;
	//    }

	//    public static bool TryParse( string fullName, out SchemaName schema )
	//    {
	//        schema = DataObjectNameParser.TryParse( fullName, ( ns, name ) => new SchemaName( name, ns ) );
	//        return schema != null;
	//    }

	//    public static SchemaName Parse( string fullName )
	//    {
	//        SchemaName name;
	//        if ( SchemaName.TryParse( fullName, out name ) )
	//            return name;
	//        else
	//            throw new InvalidOperationException( String.Format( "O schemaname \"{0}\" é inválido.", fullName ) );
	//    }
	//}
}
